package com.summba.corpus.extractor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FeatureExtraction {
	
	private List<String> setCorpus = new ArrayList<String>();
	private Set<String> setKeyword = new HashSet<String>();
	private Map<String,Integer> mapWordRate = new HashMap<String,Integer>(); 
	private String[] strRule ;
	
	
	/**
	 * @author Mark Liang
	 * @param
	 */
	public FeatureExtraction( ){
		//setCorpus = setCorpusContain;
	}
	
	/*
	 * 特征搭配规律： 
	 * /v /n 
	 * v + v
	 * a + n
	 * a + v
	 * n + n
	 * n + a
	 * n + n + n
	 */
	public Set<String> assembleKeyWords(List<String> setCorpusContain ,String rule){
		setCorpus = setCorpusContain;
		//Set<String> setKeyword = new HashSet<String>();
		strRule = rule.split(" ");
		for (Iterator<String> iterator = setCorpus.iterator(); iterator.hasNext();) {
			String strCorpus = (String) iterator.next();
			String[] tempCorpus = strCorpus.split(" "); 
			for (int i = 0; i < tempCorpus.length; i++) {
				//String tempStr = tempCorpus[i];
				String e = "";
				for (int j = 0; j < strRule.length; j++) {
					//System.out.println("strRule[j] :"+strRule[j]);
					if( (i+j) >= tempCorpus.length ){
						break;
					}
					if( tempCorpus[i+j].indexOf(strRule[j], 0) == -1){
						break;
					}else{
						//System.out.println("tempCorpus[i+j] :"+tempCorpus[i+j]);
						e = e + clearTag(tempCorpus[i+j]);
						if( j == strRule.length-1 ){
							System.out.println("e : "+e);
							setKeyword.add(e);
							break;
						}
					}
				}
				//System.out.println("!!"+strRule[0]);
			}
			
		}
		System.out.println("The Rule is : "+rule);
		return setKeyword;
	}

	/**
	 * 
	 * @param keyword
	 * @return int
	 * 
	 */
	/*
	private int checkPartOfSpeech(String keyword){
		int reNum = 0;
		if(keyword.indexOf("/n",0) != -1){
			reNum = 1;
		}
		if(keyword.indexOf("/v",0) != -1){
			reNum = 2;
		}
		if(keyword.indexOf("/a",0) != -1){
			reNum = 3;
		}			
		return reNum;
	}
	*/
	
	private String clearTag(String keyword){
		String reString;
		int index = 0;
		index = keyword.indexOf("/", 0);
		reString = keyword.substring(0,index);
		return reString;
	}
	
	public Map<String,Integer> statisticsWordRate(List<String> setCorpusContain , Set<String> setKeywords){
		setCorpus = setCorpusContain;
		setKeyword = setKeywords;
		for (Iterator<String> iterator = setKeyword.iterator();
		iterator.hasNext();) {
			String keyword = (String) iterator.next();
			Integer iCount = 0;
			for (Iterator<String> iterator2 = setCorpus.iterator(); iterator2
					.hasNext();) {
				String string = (String) iterator2.next();
				int i =0;
				while(true){
					i = string.indexOf(keyword, i)+1;
					if(i<1){
						break;						
					}
					iCount++;
				}
			}
			//System.out.println("key is "+keyword+"|val is "+iCount);
			mapWordRate.put(keyword, iCount);
		}
		return mapWordRate;
	}
	
}
